<?php

namespace app\common\command;

use addons\shopro\service\Wallet as WalletService;
use app\admin\model\Children;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\DividendLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use app\common\model\shopro\ReleaseAccelerateMarketplaceLogModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Partnerdividend extends Command
{
    protected function configure()
    {
        $this->setName('partnerdividend')->setHelp('新加权分红')->setDescription('新加权分红');
    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel       = new Order();
        $userModel        = new \app\common\model\User();
        $partner_day      = config('site.partner_day') - 1;
        $partner_scale    = config('site.partner_scale') / 100;
        $userData         = $userModel->where('is_partner', '=', 1)->where('score', '>', 0)->select();
        $dividendLogModel = new DividendLog();
        $info             = $dividendLogModel->where(['dividend_type' => 2])->whereTime('createtime', 'd')->find();
        if ($userData && $partner_day && $partner_scale) {
            $end_time     = strtotime(date('Y-m-d 23:59:59'));
            $sta_time     = strtotime('-'.$partner_day.' days', strtotime(date('Y-m-d')));
            $order_amount = $orderModel->whereNull('activity_type')->whereIn('status',
                    'paid,completed')->whereBetween('createtime', [$sta_time, $end_time])->sum('order_amount') ?? 0;
            $fen_money    = bcmul($order_amount, $partner_scale, 2);
            if ($fen_money > 0) {
                Db::startTrans();
                try {
                    $personage   = $fen_money / count($userData);
                    $golen_scale = config('site.golen_scale') / 100;        //金豆比例
                    $slive_scale = (100 - config('site.golen_scale')) / 100;//银豆比例
                    $user_ids    = '';
                    $all_golden  = 0;
                    $all_slive   = 0;
                    $all_score   = 0;
                    foreach ($userData as $k => $v) {
                        if ($v->score < $personage) {
                            $personage = $v['score'];
                        }
                        $all_score += $personage;
                        $user_ids  .= ','.$v->id;
                        WalletService::change($v->id, 'score', -$personage, 'jiaquan_release', [],
                            '加权分红');
                        //金币
                        $golden = bcmul($personage, $golen_scale, 2);
                        if ($golden > 0) {
                            WalletService::change($v->id, 'golden', $golden, 'jiaquan_golden', [], '积分补贴');
                            $all_golden += $golden;
                        }
                        $slive = bcmul($personage, $slive_scale, 2);
                        if ($slive > 0) {
                            //银币
                            WalletService::change($v->id, 'silver', $slive, 'jiaquan_slive', [], '加权分红');
                            $all_slive += $slive;
                        }
                    }
                    $dividendLogModel->create([
                        'user_ids'       => $user_ids,
                        'user_num'       => count($userData),
                        'actual_receipt' => $fen_money,
                        'remaining'      => $fen_money - $all_score,
                        'all_score'      => $all_score,
                        'all_golden'     => $all_golden,
                        'all_slive'      => $all_slive,
                        'dividend_type'  => 2
                    ]);
                    Db::commit();
                    echo '操作成功';
                } catch (HttpResponseException $e) {
                    Db::rollback();
                    $data    = $e->getResponse()->getData();
                    $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                    format_log_error($e, 'Release.release.HttpResponseException', $message);
                }
            } else {
                echo "无分成";
            }
        } else {
            echo '无数据';
        }
    }

}
